home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr48 / btv115.zip / EXAMPLE2.PAS < prev    next >
Pascal/Delphi Source File  |  1993-04-01  |  5KB  |  163 lines

  1. {$X+}
  2. {$V-}
  3. { EXAMPLE2.PAS - demonstrate file creation with segmented keys,
  4.                  supplemental indexes, alternate collating sequences
  5.  
  6.   Requires Turbo Pascal version 6.0
  7. }
  8. Uses
  9.   Crt,
  10.   Dos,
  11.   Btrv6,
  12.   Btv;
  13.  
  14.  
  15. type
  16.   ErrorType = Object(ErrorDisplay)
  17.     Function    Display(Error     : Integer;
  18.                         ErrorMsg  : String;
  19.                         OpCode    : Byte;
  20.                         OpCodeMsg : String;
  21.                         FileName  : PathStr
  22.                         ): ErrorAction;             Virtual;
  23.   end;
  24.  
  25.  
  26. var
  27.   F           : BtrieveFile;
  28.   Buff        : record
  29.                   Name    : String[30];
  30.                   Number  : Integer;
  31.                   Comment : String[80];
  32.                 end;
  33.   Name        : String[30];
  34.   Number      : Integer;
  35.   Comment     : String[80];
  36.   ErrHandler  : ErrorHandler;
  37.   ErrDisplay  : ErrorType;
  38.  
  39.  
  40. { Heres our error display object  }
  41. Function ErrorType.Display(Error     : Integer;
  42.                            ErrorMsg  : String;
  43.                            OpCode    : Byte;
  44.                            OpCodeMsg : String;
  45.                            FileName  : PathStr
  46.                            ): ErrorAction;
  47.   begin
  48.     ClrScr;
  49.     Writeln('Btrieve IO error for ' + FileName);
  50.     Writeln(Error,  ' - ', ErrorMsg);
  51.     Writeln(Opcode, ' - ', OpCodeMsg);
  52.     Writeln('Press any key ....');
  53.     ReadKey;
  54.     Display := erDone;  { just let the program continue }
  55.     ClrScr;
  56.   end;
  57.  
  58. begin
  59.   { first make a error display }
  60.   ErrDisplay.Init;
  61.   { now make an error handler, it needs a display object  }
  62.   ErrHandler.Init(@ErrDisplay);
  63.  
  64.   ClrScr;
  65.   Writeln('Creating a file called TEST2.DAT');
  66.  
  67.   { init the file passing it the error handler and }
  68.   { address of our data buffer                     }
  69.   F.Init('TEST2.DAT', @ErrHandler, @Buff, SizeOf(Buff));
  70.  
  71.   { the first thing to do is define the key }
  72.   { this one will be a segmented key        }
  73.   { Segment #1 is a string that is left justified and padded }
  74.   F.AddKeySegment(1, 31, bExtended + bSegmented, bLstring, 0, bLJustify);
  75.   { Segment #2  is an integer and cannot be justified at all }
  76.   F.AddKeySegment(32, 2, bExtended, bInteger, 0, bNormal);
  77.  
  78.   { now that the key is defined lets create and open it }
  79.   { we will preallocate 15 pages just for fun           }
  80.   F.Create(bPreallocate, SizeOf(Buff), 1024, 15, bNormal);
  81.   F.Open(bNormal, '');
  82.  
  83.   { lets add a couple records  }
  84.   Buff.Name   := 'AAAAAAAAAA';
  85.   Buff.Number := 1;
  86.   Buff.Comment:= 'Record #1';
  87.   F.Insert;
  88.   Write('Adding some records .');
  89.   Delay(500);
  90.  
  91.   Buff.Name   := 'BBBBBBBBBB';
  92.   Buff.Number := 2;
  93.   Buff.Comment:= 'Record #2';
  94.   F.Insert;
  95.   Write('.');
  96.   Delay(500);
  97.  
  98.   Buff.Name   := 'CCCCCCCCCC';
  99.   Buff.Number := 3;
  100.   Buff.Comment:= 'Record #3';
  101.   F.Insert;
  102.   Write('.');
  103.   Delay(500);
  104.  
  105.   Buff.Name   := 'DDDDDDDDDD';
  106.   Buff.Number := 4;
  107.   Buff.Comment:= 'Record #4';
  108.   F.Insert;
  109.   Write('.');
  110.   Delay(500);
  111.  
  112.   Buff.Name   := 'EEEEEEEEEE';
  113.   Buff.Number := 5;
  114.   Buff.Comment:= 'Record #5';
  115.   F.Insert;
  116.   Writeln('.');
  117.  
  118.   { let's see how big the file is }
  119.   Writeln('There are ', F.NumberOfRecords, ' records in the file.');
  120.   Writeln('Press a key...');
  121.   ReadKey;
  122.  
  123.   { how about reading a record by key }
  124.   Writeln('Reading by key');
  125.   { build the key }
  126.   { this will be justified and padded automatically as needed }
  127.   Name    := 'EEEEEEEEEE';
  128.   Number  := 5;
  129.   { make sure these are in the right order  }
  130.   F.MakeKey(@Name, @Number, nil, nil, nil, nil);
  131.   { read it without locks }
  132.   F.Get(bGetEqual, bNoLock);
  133.   Writeln(Buff.Name);
  134.   Writeln(Buff.Number);
  135.   Writeln(Buff.Comment);
  136.   Writeln('Press a key...');
  137.   ReadKey;
  138.  
  139.   { add a supplemental key this is just like adding a normal key  }
  140.   { the new index will have an alternate collating sequence       }
  141.   { this key should not be justified since existing data won't be }
  142.   F.AddSupplKeySegment(34, 81, bExtended + bAltSequence, bLstring, 0, bNormal);
  143.   { if the file already had an alternate collating sequence }
  144.   { we would skip this next call                            }
  145.   F.AddAltSequence('UPPER.ALT');
  146.   F.CreateIndex;
  147.  
  148.   { read with the new index }
  149.   F.SetKeyPath(1);
  150.   { see if UPPER.ALT works }
  151.   Comment := 'ReCoRd #1';
  152.   F.MakeKey(@Comment, nil, nil, nil, nil, nil);
  153.   F.Get(bGetEqual, bNoLock);
  154.   Writeln(Buff.Name);
  155.   Writeln(Buff.Number);
  156.   Writeln(Buff.Comment);
  157.   Writeln('Press a key...');
  158.   ReadKey;
  159.  
  160.   { now get rid of the index  }
  161.   F.DropIndex(1);
  162.   F.Close;
  163. end.